#include <stdio.h>
#include <time.h>
#define N 10

__global__ void add(int *a, int *b, int *c) {
	int tid=blockIdx.x;
	if(tid<N) {
		c[tid]=a[tid]+b[tid];
	}
}

int main(void) {
	int *a,*b,*c,*x,*y,i;
	int *dev_a, *dev_b, *dev_c;
	int size = N * sizeof(int);

	srand((unsigned int)time((time_t *)NULL));

	cudaMalloc((void**) &dev_a, size);
	cudaMalloc((void**) &dev_b, size);
	cudaMalloc((void**) &dev_c, size);

	a=(int *)malloc(N*sizeof(int));
	b=(int *)malloc(N*sizeof(int));
	c=(int *)malloc(N*sizeof(int));

	x=a;
	y=b;
	for(i=0;i<N;i++) {
		*a=(rand() % 50) + 50;
		*b=(rand() % 50) + 50;
		a++;
		b++;
	}
	a=x; b=y;

	cudaMemcpy(dev_a, a, size, cudaMemcpyHostToDevice);
	cudaMemcpy(dev_b, b, size, cudaMemcpyHostToDevice);

	add<<<N,1>>>(dev_a, dev_b, dev_c);

	cudaMemcpy(c, dev_c, size, cudaMemcpyDeviceToHost);
	
	printf("Tampilkan hasil\n");
	for(i=0;i<N;i++) {
		printf("%d+%d=%d\n",a[i],b[i],c[i]);
	}	

	free(a); free(b); free(c);
	cudaFree(dev_a);
	cudaFree(dev_b);
	cudaFree(dev_c);
	return 0;
}
